package com.fzu.messageservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzu.commonutil.ImResult;
import com.fzu.commonutil.entity.ImChatUserMessage;
import com.fzu.commonutil.entity.User;
import com.fzu.commonutil.mapper.ImChatUserMessageMapper;
import com.fzu.commonutil.util.CommonConst;
import com.fzu.commonutil.util.ImUtil;
import com.fzu.messageservice.client.NormalClient;
import com.fzu.messageservice.service.ImChatUserMessageService;
import com.fzu.messageservice.vo.UserMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 单聊记录(ImChatUserMessage)表服务实现类
 *
 * @author Answer
 * @since 2023-04-04 21:45:58
 */
@Service("imChatUserMessageService")
public class ImChatUserMessageServiceImpl extends ServiceImpl<ImChatUserMessageMapper, ImChatUserMessage> implements ImChatUserMessageService {

    @Autowired
    private NormalClient normalClient;


    @Override
    public ImResult<Page> listFriendMessage(Long current, Long size, Integer friendId) {
        Page<ImChatUserMessage> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);

        Integer userId = ImUtil.getUserId();

        LambdaQueryWrapper<ImChatUserMessage> lambdaQuery = new LambdaQueryWrapper<>();
//                imChatUserMessageService.lambdaQuery();
        lambdaQuery.and(wrapper -> wrapper.eq(ImChatUserMessage::getFromId, userId).eq(ImChatUserMessage::getToId, friendId))
                .or(wrapper -> wrapper.eq(ImChatUserMessage::getFromId, friendId).eq(ImChatUserMessage::getToId, userId));
        lambdaQuery.orderByDesc(ImChatUserMessage::getCreateTime);
        page(page, lambdaQuery);
        List<ImChatUserMessage> records = page.getRecords();
//        List<ImChatUserMessage> imChatUserMessages = page.getRecords();
//        Page<ImChatUserMessage> result = lambdaQuery.page(page);
//        List<ImChatUserMessage> records = result.getRecords();
        Collections.reverse(records);
        if (CollectionUtils.isEmpty(records)) {
            return ImResult.success(page);
        } else {
            List<UserMessageVO> collect = records.stream().map(message -> {
                UserMessageVO userMessageVO = new UserMessageVO();
                userMessageVO.setContent(message.getContent());
                userMessageVO.setFromId(message.getFromId());
                userMessageVO.setToId(message.getToId());
                userMessageVO.setMessageStatus(message.getMessageStatus());
                userMessageVO.setId(message.getId());
                userMessageVO.setCreateTime(message.getCreateTime());
                //写到这
//                User from = normalClient.getUserById(message.getFromId().toString());
//                if (from != null) {
//                    userMessageVO.setAvatar(from.getAvatar());
//                }
                return userMessageVO;
            }).collect(Collectors.toList());
            Page<UserMessageVO> resultVO = new Page<>();
            resultVO.setRecords(collect);
            resultVO.setTotal(page.getTotal());
            resultVO.setCurrent(page.getCurrent());
            resultVO.setSize(page.getSize());
            return ImResult.success(resultVO);
        }
    }

    @Override
    public ImResult<Integer> getUnReadMessage() {
        Integer userId = ImUtil.getUserId();
        LambdaQueryWrapper<ImChatUserMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ImChatUserMessage::getToId, userId);
        wrapper.eq(ImChatUserMessage::getMessageStatus, CommonConst.UNREAD_STATUS);
        int count = count(wrapper);
        return ImResult.success(count);
    }

}
